<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="success" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="menus" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
				  style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="160" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100"  sortable>
				<template slot-scope="scope">
					<span style="color: red" v-if="scope.row.state==false">禁用</span>
					<span style="color: green" v-else-if="scope.row.state==true">启用</span>
					<span style="color: grey" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="component" label="组件路径" width="130" sortable></el-table-column>
			<el-table-column prop="url" label="当前组件的path路径" width="150" sortable></el-table-column>
			<el-table-column prop="icon" label="图标" width="100" sortable></el-table-column>
			<el-table-column prop="state" label="父级菜单" width="100"  sortable>
				<template slot-scope="scope">
					<span style="color: blue" v-if="scope.row.parent">{{scope.row.parent.name}}</span>
					<span style="color: yellowgreen" v-else>无上级</span>
				</template>
			</el-table-column>
			<el-table-column prop="intro" label="简介" min-width="150" sortable></el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" type="primary"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSizes" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑/新增界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="上级菜单">
					<!--
                        :options="options" - 数据 - 无限极树- 集合
                            label:'name' - 回显哪一个属性值
                            value:'id' - 传值，将id传递给v-model="saveForm.parent_id"
                    -->
					<el-cascader v-model="saveForm.parentId"
								 :options="menuTree"
								 :props="{
							checkStrictly: true,
							label: 'name',
							value: 'id'
						}" clearable>
					</el-cascader>
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="排序">
					<el-input-number v-model="saveForm.index" :min="0" :max="200"></el-input-number>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="radio" :label="true">启用</el-radio>
						<el-radio class="radio" :label="false">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="访问url" >
					<el-input v-model="saveForm.url" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="组件路径" prop="component">
					<el-input v-model="saveForm.component" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="图标" >
					<el-input v-model="saveForm.icon" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="简介" >
					<el-input v-model="saveForm.intro" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getMenuListPage, removeMenu, batchRemoveMenu, saveMenu, addMenu } from '../../api/api';

	export default {
		data() {
			return {
                menus: [],
                menuTree:[],//部门树
                totals: 0,//总数
                pageSizes:5,//每页数量
                currentPage:1,//页数
                listLoading: false,//加载框

				//高级查询参数
				keyword:'',

				page: 1,

				sels: [],//列表选中列

				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
				saveFormRules: {
                    name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
                    component: [
                        { required: true, message: '请输入路径', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				saveForm: {
					id: 0,
                    name: '',
                    component: '',
                    url: '',
                    icon: '',
                    index: null,
                    parentId:null,
                    intro:'',
                    state:null
				}

			}
		},
		methods: {
		    //高级查询
			keywordQuery(){
			    this.currentPage = 1;
			    this.getMenus()
			},
		    //分页查询
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getMenus();
            },
			//获取员工列表
			getMenus() {
				let para = {
                    pageSizes: this.pageSizes,
                    currentPage: this.currentPage,
					keyword:this.keyword
				};
				//开启加载框
				this.listLoading = true;
				//发送请求
				this.$http.post("/menu",para).then(res=>{
				    //给menus数组赋值
				    this.menus = res.data.data;
				    //给分页赋值
                    this.totals = res.data.totals;
                    //关闭加载框
                    this.listLoading = false;
				}).catch(res=>{
					this.$message.error("系统繁忙，正在骚扰程序猿")
				})

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//用ajax发送请求
					this.$http.delete("/menu/"+row.id).then(res=>{
                        this.listLoading = false;
                        //判断执行是否成功
					    if (res.data.success){
					        this.$message.success("我们胜利了")
                            //判断当前也是否有数据
                            //如果当前页没有数据，那就跳转到前一页
                            if (this.menus.length == 1 && this.currentPage != 1) {
                                this.currentPage = this.currentPage - 1;
                            }
                            this.getMenus();
						}else {
					        this.$message.error("程序猿要加班了【500】")
						}
					}).catch(res=>{
					    this.$message.error("【400.404】警告进入未知区域")
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
                this.title = "编辑";
                //获取部门树
                this.getmenuTree();
                //开启会话框
				this.saveFormVisible = true;
				//回显数据
				this.saveForm = Object.assign({}, row);
				console.log(this.saveForm.state)
			},
			//显示新增界面
			handleAdd: function () {
                this.title = "新增";
                //获取部门树
                this.getmenuTree();
                //开启会话框
				this.saveFormVisible = true;
				this.saveForm = {
                    id: '',
                    name: '',
                    component: '',
                    url: '',
                    icon: '',
                    index: '',
                    parentId:'',
                    intro:'',
                    state:false
				};
			},
			//编辑
            saveSubmit: function () {
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.saveLoading = true;
                            let para = Object.assign({}, this.saveForm);//数据赋值
                            //对上级部门的处理
                            var result = para.parentId;

                            //5种情况，不修改值为2，点击后不修改【2】，去掉上级部门【】，选择子部门【2,3,4】，直接清空null
                            if (Array.isArray(result)){
                                if (result.length==null){//空的
                                    para.parentId = null;
                                }else {//有数据就获取数据
                                    para.parentId = result[result.length -1];
                                }
                            }
                            this.$http.put("/menu",para).then(res=>{
                                this.saveLoading = false;//关闭忙等框
                                this.saveFormVisible = false;//关闭会话框
                                if (res.data.success){
                                    this.$message.success("保存成功");
                                    this.getMenus()
                                }else {
                                    this.$message.error("毁灭把，赶紧的【500】")
                                }
                            }).catch(rse=>{
                                this.$message.error("啊哈哈哈哈，出错了【400.404】")
                            })
                        });
                    }
                });
            },
			//多选框绑定数据
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//ajax发送请求
					this.$http.patch("/menu",ids).then(res=>{
					    this.listLoading = false;
					    //判断成功没
						if (res.data.success){
						    this.currentPage = 1;
						    this.$message.success("我他妈莱依拉")
							this.getMenus()
						}else {
						    this.$message.error("你这是要我去死【500】")
						}
					}).catch(res=>{
					    this.$message.error("我不活了【400.404】")
					})
				}).catch(() => {

				});
			},
            //获取部门树
            getmenuTree(){
                this.$http.get("/menu/menuTree").then(res=>{
                    this.menuTree = res.data;
                })
            },
		},
		mounted() {
			this.getMenus();
		}
	}

</script>

<style scoped>

</style>